package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Genericvalue;

@Repository(value = "genericValueDao")
@Transactional(readOnly = false)
public class GenericValueDaoImpl implements GenericValueDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Genericvalue> getListGenericValue() {
		TypedQuery<Genericvalue> q = em.createNamedQuery("Genericvalue.findAll",Genericvalue.class);
    	return q.getResultList();
	}

	public List<Genericvalue> getListGenericValueByPharmacyId(Integer pharmacyId) {
		TypedQuery<Genericvalue> q = em.createNamedQuery("Genericvalue.findByPharmacyId",Genericvalue.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	return q.getResultList();
	}

	public List<Genericvalue> getListGenericValueByPharmacyIdByGroup(Integer pharmacyId, Short group) {
		TypedQuery<Genericvalue> q = em.createNamedQuery("Genericvalue.findByPharmacyIdByGroup",Genericvalue.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("group",group);
    	return q.getResultList();
	}

	public List<Genericvalue> getListGenericValueByGroup(Short group) {
		TypedQuery<Genericvalue> q = em.createNamedQuery("Genericvalue.findByGroup",Genericvalue.class);
    	q.setParameter("group",group);
    	return q.getResultList();
	}

	public Genericvalue findByGenericValueId(Integer genericValueId) {
		Genericvalue res=null;
		try {
			res=em.find(Genericvalue.class,genericValueId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

	public void update(Genericvalue genericValue) {
		em.merge(genericValue);
	}

	public void remove(Genericvalue genericValue) {
		em.remove(em.getReference(Genericvalue.class,genericValue.getGenericValueId()));
	}

	public void add(Genericvalue genericValue) {
		em.persist(genericValue);
	}

}
